/**
 * 
 * @Author: biubiu0002
 * @Date: 2021-04-26 11:41
 **/




#include "ClassMapper.h"
#include "list"
#include "iostream"
#include "fstream"
#include "sstream"
#include "string"
#include "vector"
using namespace std;

list<Class> ClassMapper::loadClassList() {
    list<Class> classList;
    string line,field;
    ifstream ifs("../../resource/Class.txt",ios::in);
    if(!ifs){
        cout<<"Class.txt文件打开失败";
    }
    while(getline(ifs,line)){
        Class aClass;
        vector<string> infoList;
        istringstream iss(line);
        while(iss>>field){
            infoList.push_back(field);
        }
        aClass.setClassId(infoList[0]);
        aClass.setGrade(stoi(infoList[1]));
        aClass.setCollege(infoList[2]);
        aClass.setMajor(infoList[3]);
        aClass.setClassNo(stoi(infoList[4]));
        classList.push_back(aClass);
    }
    ifs.close();
    return classList;


}


int ClassMapper::batchInsertClassList(list<Class> &classList) {
    ofstream ofs("../../resource/Class.txt",ios::out);
    if(!ofs){
        cout<<"Course.txt文件打开失败"<<endl;
    }
    for(Class aClass:classList){
        ofs<<aClass.getClassId()<<" ";
        ofs<<aClass.getGrade()<<" ";
        ofs<<aClass.getCollege()<<" ";
        ofs<<aClass.getMajor()<<" ";
        ofs<<aClass.getClassNo()<<endl;

    }
    return 0;
}

int ClassMapper::checkId(string classId){
    list<Class> classList = this->loadClassList();
    for(Class aClass:classList){
        if(aClass.getClassId()==classId)
            return 1;
    }
    return 0;
}

const list<Class> &ClassMapper::getClassList() const {
    return classList;
}

void ClassMapper::setClassList(const list<Class> &classList) {
    ClassMapper::classList = classList;
}

ClassMapper::ClassMapper() {
    this->setClassList(this->loadClassList());
}
